/* eslint-disable camelcase */
import { ElMessage } from "element-plus";
import { computed, ref } from "vue";
import { defineStore } from "pinia";
import { IJsonData, IJsonDataContext, QuestionType } from "@/types/question";

import localforage from "@/plugins/localforage";

// 使用setup模式定义
export const questionStore = defineStore(
  "questionStore",
  () => {
    // 当前的json
    const json = ref<IJsonDataContext[]>([]);
    // 当前正在标注问题的id
    const currentId = ref<number>(0);
    const currentFileName = ref("");

    const currentItem = computed(() => {
      const item = json.value.find(
        (item) => item.context_id === currentId.value
      );
      return item;
    });

    const addQuestion = (params: {
      question: string;
      answer: string;
      question_type: QuestionType;
    }) => {
      if (
        currentItem.value?.qaList.find(
          (e) =>
            e.question === params.question &&
            e.answer === params.answer &&
            e.question_type === params.question_type
        )
      )
        return ElMessage.warning("问题已存在");
      currentItem.value?.qaList.push({
        question: params.question,
        answer: params.answer,
        question_type: params.question_type,
      });
      updateIndexDb();
    };
    const editQuestion = (params: {
      index: number;
      question: string;
      answer: string;
      question_type: QuestionType;
    }) => {
      if (!currentItem.value) return;
      currentItem.value.qaList[params.index] = {
        question: params.question,
        answer: params.answer,
        question_type: params.question_type,
      };
      updateIndexDb();
    };

    const deleteQuestion = (params: { index: number }) => {
      if (currentItem.value?.qaList[params.index]) {
        currentItem.value?.qaList.splice(params.index, 1);
      }
      updateIndexDb();
    };

    const setCurrentId = (id: number) => {
      currentId.value = id;
      updateIndexDb();
    };

    const setJson = (data: IJsonData) => {
      currentFileName.value = data.fileName;
      json.value = data.contexts;
      setCurrentId(json.value[0].context_id);
      updateIndexDb();
    };

    const totalCount = computed(() => {
      return json.value.length;
    });
    const currentIndex = computed(() => {
      return json.value.findIndex(
        (item) => item.context_id === currentId.value
      );
    });
    const previous = () => {
      if (currentIndex.value - 1 < 0)
        return ElMessage.warning("已经是第一条了");
      currentId.value = json.value[currentIndex.value - 1].context_id;
      updateIndexDb();
    };
    const next = () => {
      if (currentIndex.value + 1 >= totalCount.value)
        return ElMessage.warning("已经是最后一条了");
      currentId.value = json.value[currentIndex.value + 1].context_id;
      updateIndexDb();
    };
    const setNumber = (index: number) => {
      if (index <= 0 || index > totalCount.value)
        return ElMessage.warning("请输入正确数字");
      currentId.value = json.value[index - 1].context_id;
      updateIndexDb();
    };

    const updateIndexDb = () => {
      const data = {
        currentId: currentId.value,
        json: json.value,
        currentFileName: currentFileName.value,
      };
      localforage.setItem("store", JSON.stringify(data)).then((res) => {
        console.log("store success");
      });
    };

    const getDbData = () => {
      localforage.getItem("store").then((res) => {
        const data = JSON.parse(res as string);
        if (!data) return;
        json.value = data.json;
        currentId.value = data.currentId;
        currentFileName.value = data.currentFileName;
      });
    };
    return {
      addQuestion,
      editQuestion,
      deleteQuestion,
      json,
      setJson,
      currentId,
      currentFileName,
      setCurrentId,
      currentItem,
      previous,
      next,
      totalCount,
      currentIndex,
      setNumber,
      getDbData,
    };
  },
  {
    persist: false,
  }
);
